package com.xc.service.impl;

import com.xc.common.ServerResponse;
import com.xc.dao.SiteInfoMapper;
import com.xc.pojo.SiteInfo;
import com.xc.service.ISiteInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author mipha
 * @version 1.0
 * @date 2019/3/16 1:43 AM
 */
@Service("iSiteInfoService")
public class SiteInfoServiceImpl implements ISiteInfoService{

    @Autowired
    SiteInfoMapper siteInfoMapper;


    @Override
    public ServerResponse get() {
        List<SiteInfo> siteInfos = siteInfoMapper.findAll();
        if(siteInfos.size() > 0){
            SiteInfo siteInfo = siteInfos.get(0);
            return ServerResponse.createBySuccess(siteInfo);
        }
        return ServerResponse.createByErrorMsg("设置信息不存在");
    }

    @Override
    public ServerResponse add(SiteInfo siteInfo) {

        //  首先判断是否已经添加
        List<SiteInfo> siteInfos = siteInfoMapper.findAll();
        if(siteInfos.size() > 0){
            return ServerResponse.createByErrorMsg("不能重复添加");
        }

        if(StringUtils.isBlank(siteInfo.getSiteName())
                || StringUtils.isBlank(siteInfo.getSiteLogo())
                || StringUtils.isBlank(siteInfo.getSiteLogoSm())
                ){
            return ServerResponse.createByErrorMsg("名字和logo不能为空");
        }

        int insertCount = siteInfoMapper.insert(siteInfo);
        if(insertCount > 0){
            return ServerResponse.createBySuccessMsg("添加成功");
        }else{
            return ServerResponse.createByErrorMsg("添加失败");
        }
    }

    @Override
    public ServerResponse update(SiteInfo siteInfo) {
        if(siteInfo.getId() == null){
            return ServerResponse.createByErrorMsg("ID不能为空");
        }

        int updateCount = siteInfoMapper.updateByPrimaryKeySelective(siteInfo);
        if(updateCount > 0){
            return ServerResponse.createBySuccessMsg("修改成功");
        }else{
            return ServerResponse.createByErrorMsg("修改失败");
        }
    }

    @Override
    public ServerResponse getInfo() {
        List<SiteInfo> siteInfos = siteInfoMapper.findAll();
        if(siteInfos.size() > 0){
            SiteInfo siteInfo = siteInfos.get(0);
            return ServerResponse.createBySuccess(siteInfo);
        }
        return ServerResponse.createByErrorMsg("设置信息info不存在");
    }




}
